package com.demo.app.testvolley;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

import javax.net.ssl.HttpsURLConnection;

public class MainActivity extends Activity implements Response.Listener<String>, Response.ErrorListener, View.OnClickListener, RequestQueue.RequestFinishedListener<Object> {

    private static final String TAG = MainActivity.class.getSimpleName();
    RequestQueue requestQueue;
    String url = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";

    PriorityBlockingQueue<String> pbq = new PriorityBlockingQueue();


    AtomicInteger atomicInteger = new AtomicInteger();


    Executor mResponsePoster;
    Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        requestQueue = VolleySingleton.getInstance(this).getRequestQueue();
        //requestQueue.start();


        init();

    }


    private void init() {
        findViewById(R.id.but).setOnClickListener(this);
        findViewById(R.id.but2).setOnClickListener(this);

        findViewById(R.id.but3).setOnClickListener(this);


        for (int i = 0; i < 4; i++) {
            MyThread t = new MyThread(pbq);
            t.start();
        }


        mResponsePoster = new Executor() {
            @Override
            public void execute(Runnable command) {
                handler.post(command);
            }
        };

    }

    @Override
    public void onClick(View v) {


        switch (v.getId()) {
            case R.id.but:

                StringRequest request = new StringRequest(url, this, this);
                requestQueue.add(request);

                break;

            case R.id.but2:

                mResponsePoster.execute(new Runnable() {
                    @Override
                    public void run() {

                        Log.e(TAG, "mResponsePoster run: " + Thread.currentThread().getName());

                    }
                });
                break;
            case R.id.but3:

                //Executors.newSingleThreadExecutor()

                testExecutor();

                break;
        }


        // pbq.put(""+new Random().nextInt());

    }

    private void testExecutor() {


        ExecutorService service = Executors.newSingleThreadExecutor();


        Callable callable = new Callable<String>() {

            @Override
            public String call() throws Exception {

                Thread.sleep(2000);
                return "tom";
            }
        };

        Future<String> future = service.submit(callable);

        try {
            String val = future.get();
            Log.e(TAG, "testExecutor: " + val);
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onResponse(String response) {

        Log.e(TAG, "onResponse: " + response);

        TextView textView = findViewById(R.id.tv_content);
        textView.setText(response);
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        error.printStackTrace();
        Log.e(TAG, "onErrorResponse: ", error);
    }


    @Override
    public void onRequestFinished(Request<Object> request) {
        Log.e(TAG, "onRequestFinished: " + request.getUrl());

    }
}
